
import base64
from openai import OpenAI

client = OpenAI(
    api_key="sk-3e0b22531704473698a248579130663d",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

# 读取本地图片并编码为 Base64
image_path = r"C:\Users\fangchen\Desktop\123.png"

with open(image_path, "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read()).decode("utf-8")

# 构造 data URI
data_uri = f"data:image/jpeg;base64,{encoded_string}"

completion = client.chat.completions.create(
    model="qwen-vl-max-latest", # 此处以qwen-vl-max-latest为例，可按需更换模型名称。模型列表：https://help.aliyun.com/model-studio/getting-started/models
    messages=[
        {
            "role": "system",
            "content": [{"type": "text", "text": "You are a helpful assistant."}],
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        # "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"
                        "url": data_uri
                    },
                },
                {"type": "text", "text": "帮我找出这个图片中日期2025-04-25,贷方发生额不为0的金额,并将金额以amount=xxx的格式输出?"},
            ],
        },
    ],
)

print(completion.choices[0].message.content)